﻿// Decompiled with JetBrains decompiler
// Type: AddInManager.Tree
// Assembly: AddInManager, Version=2015.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 5BDD7A83-FA69-4D91-8F2B-9F16E915F05A
// Assembly location: D:\Autodesk\Revit 2019 SDK\Add-In Manager\AddInManager.dll

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace FsAddInManager
{
    [Designer(typeof(TreeDesigner))]
    public partial class Tree : TreeView
    {
        public const string UNCHECKED_KEY = "Unchecked.bmp";
        public const string CHECKED_KEY = "Checked.bmp";
        private ImageList stateImageList;
        private IContainer components;
        private Node m_activeNode;

        public Tree()
        {
            this.InitializeComponent();
            this.StateImageList = this.stateImageList;
        }

        public Node GetNodeAt(string nodeFullPath)
        {
            if (string.IsNullOrEmpty(nodeFullPath))
            {
                return (Node) null;
            }

            Node foundNode = null;
            string str1 = nodeFullPath;
            string[] separator = new string[1]
            {
                this.PathSeparator
            };


            TreeNodeCollection nodes = this.Nodes;
            foreach (string str2 in str1.Split(separator, StringSplitOptions.RemoveEmptyEntries))
            {
                foreach (Node node2 in nodes)
                {
                    if (node2.Text == str2)
                    {
                        foundNode = node2;
                        nodes = foundNode.Nodes;
                    }
                }
            }

            return foundNode;
        }

        public Node GetNodeByTxt(string strCmd)
        {
            if (string.IsNullOrEmpty(strCmd))
            {
                Debug.Assert(false);
                return null;
            }

            string str1 = strCmd;


            TreeNodeCollection nodes = this.Nodes;
            Node foundNode = null;
            foreach (Node nL1 in nodes)
            {
                var nL1Nodes = nL1.Nodes;
                foreach (TreeNode nL2 in nL1Nodes)
                {
                    string noteTxt = MainForm.GetCmdName(nL2.Text);
                    if (String.Compare(noteTxt.ToLower(), strCmd.ToLower(), StringComparison.Ordinal) == 0)
                    {
                        foundNode = nL2 as Node;

            
                        break;
                    }
                }
            }

            return foundNode;
        }


        public Node ActiveNode
        {
            get => this.m_activeNode;
            set
            {
                if (this.m_activeNode == value)
                    return;
                this.m_activeNode = value;
                if (this.ActiveNodeChanged == null)
                    return;
                this.ActiveNodeChanged((object) null, (EventArgs) null);
            }
        }

        public event EventHandler ActiveNodeChanged;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            TreeViewHitTestInfo treeViewHitTestInfo = this.HitTest(e.Location);
            if (e.Button != MouseButtons.Left || treeViewHitTestInfo.Location != TreeViewHitTestLocations.StateImage)
                return;
            Node node = treeViewHitTestInfo.Node as Node;
            string str = node.StateImageKey.Equals("Unchecked.bmp") ? "Checked.bmp" : "Unchecked.bmp";
            node.StateImageKey = str;
            node.Save = node.Checked = node.StateImageKey.Equals("Checked.bmp");
            this.UpdateChildren(node);
            this.UpdateParent(node);
            this.ActiveNode = node;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            int keyChar = (int) e.KeyChar;
        }

        private void UpdateChildren(Node node)
        {
            foreach (Node node1 in node.Nodes)
            {
                if (!node1.StateImageKey.Equals(node.StateImageKey))
                {
                    node1.StateImageKey = node.StateImageKey;
                    node.Save = node1.Checked = node1.StateImageKey.Equals("Checked.bmp");
                }

                this.UpdateChildren(node1);
            }
        }

        private void UpdateParent(Node node)
        {
            if (node == null || node.Parent == null)
                return;
            bool flag = true;
            string stateImageKey = node.StateImageKey;
            foreach (TreeNode node1 in node.Parent.Nodes)
            {
                if (!node1.StateImageKey.Equals(stateImageKey))
                {
                    node.Parent.StateImageKey = "Checked.bmp";
                    node.Parent.Save = node.Parent.Checked = true;
                    return;
                }
            }

            if (!flag)
                return;
            node.Parent.StateImageKey = stateImageKey;
            node.Parent.Save = node.Parent.Checked = node.Parent.StateImageKey.Equals("Checked.bmp");
            this.UpdateParent(node.Parent);
        }

        protected void InitializeComponent()
        {
            this.components = (IContainer) new Container();
            ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(Tree));
            this.stateImageList = new ImageList(this.components);
            this.SuspendLayout();
            this.stateImageList.ImageStream = (ImageListStreamer) componentResourceManager.GetObject("stateImageList.ImageStream");
            this.stateImageList.TransparentColor = Color.Transparent;
            this.stateImageList.Images.SetKeyName(0, "Unchecked.bmp");
            this.stateImageList.Images.SetKeyName(1, "Checked.bmp");
            this.ResumeLayout(false);
        }
    }
}